package com.zmn.oms.admin.thymeleaf;

import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.oms.common.constant.OrderPremissionConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 类描述: 订单权限
 *
 * @author liuying
 * @date 2020/07/13 5:03 下午
 */
@Slf4j
public class OrderAuthPermit {

    public static OrderAuthPermit getInstance() {
        return new OrderAuthPermit();
    }

    /**
     * 订单权限
     *
     * @param plat
     * @param permit
     * @return
     */
    public boolean checkPermitByPlat(Integer plat, String permit) {
        if (Objects.isNull(plat)) {
            log.debug("checkDetailPermit-->没有获取到 url plat={}, permit={}", plat, permit);
            return false;
        }

        Integer moduleId = OrderPremissionConsts.getModuleIdByPlat(plat);
        return checkPermit(moduleId, permit);
    }

    /**
     * 订单详情校验权限
     *
     * @param moduleId
     * @param permit
     * @return
     */
    public boolean checkDetailPermit(Integer moduleId, String permit) {
        String permitUrl = OrderPremissionConsts.getDetailPermitUrl(moduleId, permit);
        if (StringUtils.isBlank(permitUrl)) {
            log.debug("checkDetailPermit-->没有获取到 url moduleId={}, permit={}", moduleId, permit);
            return true;
        }
        boolean isPermitted = StaffAdminService.isPermitted(permitUrl);
        log.debug("checkDetailPermit->moduleId={}, permitUrl={}, isPermitted={}", moduleId, permitUrl, isPermitted);
        return isPermitted;
    }

    /**
     * 校验权限
     *
     * @param moduleId
     * @param permitUrl
     * @return
     */
    public boolean checkPermit(Integer moduleId, String permitUrl) {
        // log.info("checkPermit->plat={}permit={}", moduleId, permitUrl);
        // 获取路径
        String modulePath = OrderPremissionConsts.PERMIT_MODULE_BASE_PATH.get(moduleId);
        if (Objects.isNull(modulePath)) {
            modulePath = "";
        }
        String fullPermitUrl = OrderPremissionConsts.getPermitUrlByModuleId(moduleId, permitUrl);
        boolean isPermitted = StaffAdminService.isPermitted(fullPermitUrl);
        return isPermitted;
    }

}
